package cn.SiFuLan.dao.impl;

import cn.SiFuLan.dao.IspuDao;
import cn.SiFuLan.entity.brandEntity;
import cn.SiFuLan.entity.classifyEntity;
import cn.SiFuLan.entity.spuEntity;
import cn.SiFuLan.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class spuDaoImpl extends BaseDao implements IspuDao {
    public List<spuEntity> all() {
        String sql="SELECT s.Spu_id,s.Spu_Name,s.title,c.Classid,c.Class_Name,b.Brand_Info,spuimg,s.Spu_info,s.Packing,s.service,s.Takedown,s.AddTime,s.UpdateTime FROM spu s INNER JOIN classify c on s.Class_id=c.Classid INNER JOIN brand b on s.Brand_id=b.Brandid ";
        List<spuEntity> list=new ArrayList<>();
        sql+=" order by s.Spu_id asc";
        ResultSet rs= executeQuery(sql,null);
        while (true) {
            try {
                if (!rs.next()) break;
                spuEntity spu=new spuEntity();
                spu.setSpu_id(rs.getInt("Spu_id"));
                spu.setSpu_Name(rs.getString("Spu_Name"));
                spu.setTitle(rs.getString("title"));
                classifyEntity c =new classifyEntity();
                c.setClass_Name(rs.getString("Class_Name"));
                spu.setClassify(c);
                brandEntity b=new brandEntity();
                b.setBrand_Info(rs.getString("Brand_Info"));
                spu.setBrand(b);
                spu.setSpuimg(rs.getString("spuimg"));
                spu.setSpu_Info(rs.getString("Spu_info"));
                spu.setPacking(rs.getString("Packing"));
                spu.setService(rs.getString("service"));
                spu.setTakedown(rs.getInt("Takedown"));
                spu.setAddTime(rs.getDate("AddTime"));
                spu.setUpdateTime(rs.getDate("UpdateTime"));
                list.add(spu);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<spuEntity> aall(spuEntity spu) {
        String sql="SELECT s.Spu_id,s.Spu_Name,s.title,c.Class_Name,b.Brand_Info,spuimg,s.Spu_info,s.Packing,s.service,s.Takedown,s.AddTime,s.UpdateTime FROM spu s INNER JOIN classify c on s.Class_id=c.Classid INNER JOIN brand b on s.Brand_id=b.Brandid   where 1=1  ";
        List<spuEntity> list=new ArrayList<>();
        if(spu!=null && !spu.equals("")){
            if(spu.getSpu_Name()!=null && !spu.equals("")){
                sql+=" and Spu_Name like'%"+spu.getSpu_Name()+"%'";
            }
            if(spu.getBrand_id()>0){
                sql+=" and c.Classid ="+spu.getBrand_id();
            }
        }
        sql+=" order by s.Spu_id asc";
        ResultSet rs= executeQuery(sql,null);
        while (true) {
            try {
                if (!rs.next()) break;
                spuEntity s=new spuEntity();
                s.setSpu_id(rs.getInt("Spu_id"));
                s.setSpu_Name(rs.getString("Spu_Name"));
                s.setTitle(rs.getString("title"));
                classifyEntity c =new classifyEntity();
                c.setClass_Name(rs.getString("Class_Name"));
                s.setClassify(c);
                brandEntity b=new brandEntity();
                b.setBrand_Info(rs.getString("Brand_Info"));
                s.setBrand(b);
                s.setSpuimg(rs.getString("spuimg"));
                s.setSpu_Info(rs.getString("Spu_info"));
                s.setPacking(rs.getString("Packing"));
                s.setService(rs.getString("service"));
                s.setTakedown(rs.getInt("Takedown"));
                s.setAddTime(rs.getDate("AddTime"));
                s.setUpdateTime(rs.getDate("UpdateTime"));
                list.add(s);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean add(spuEntity spuEntity) {
        String sql="INSERT INTO spu(Spu_Name,title,Class_id,Brand_id,spuimg,Spu_Info,Packing,service,Takedown,AddTime,UpdateTime)VALUES (?,?,?,?,?,?,?,?,?,?,?)";
        List<Object> sz=new ArrayList<>();
        sz.add(spuEntity.getSpu_Name());
        sz.add(spuEntity.getTitle());
        sz.add(spuEntity.getClass_id());
        sz.add(spuEntity.getBrand_id());
        sz.add(spuEntity.getSpuimg());
        sz.add(spuEntity.getSpu_Info());
        sz.add(spuEntity.getPacking());
        sz.add(spuEntity.getService());
        sz.add(spuEntity.getTakedown());
        sz.add(spuEntity.getAddTime());
        sz.add(spuEntity.getUpdateTime());
        return executeUpdate(sql,sz);
    }

    @Override
    public spuEntity getall(int spuid) {
        String sql="SELECT s.Spu_id,s.Spu_Name,s.title,c.Classid,c.Class_Name,b.Brand_Info,spuimg,s.Spu_info,s.Packing,s.service,s.Takedown,s.AddTime,s.UpdateTime FROM spu s INNER JOIN classify c on s.Class_id=c.Classid INNER JOIN brand b on s.Brand_id=b.Brandid where s.Spu_id = ? ";
        spuEntity spu=new spuEntity();
        sql+=" order by s.Spu_id asc";
        List<Object> list=new ArrayList<>();
        list.add(spuid);
        ResultSet rs= executeQuery(sql,list);
        while (true) {
            try {
                if (!rs.next()) break;
                spu.setSpu_id(rs.getInt("Spu_id"));
                spu.setSpu_Name(rs.getString("Spu_Name"));
                spu.setTitle(rs.getString("title"));
                classifyEntity c =new classifyEntity();
                c.setClass_Name(rs.getString("Class_Name"));
                spu.setClassify(c);
                brandEntity b=new brandEntity();
                b.setBrand_Info(rs.getString("Brand_Info"));
                spu.setBrand(b);
                spu.setSpuimg(rs.getString("spuimg"));
                spu.setSpu_Info(rs.getString("Spu_info"));
                spu.setPacking(rs.getString("Packing"));
                spu.setService(rs.getString("service"));
                spu.setTakedown(rs.getInt("Takedown"));
                spu.setAddTime(rs.getDate("AddTime"));
                spu.setUpdateTime(rs.getDate("UpdateTime"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return spu;
    }

    @Override
    public boolean update(spuEntity spu) {
        String sql="UPDATE spu SET Spu_Name=?,title=?,Class_id=?,Brand_id=?,spuimg=?,Spu_info=?,Packing=?,service=?,Takedown=?,UpdateTime=? WHERE Spu_id=?";
        List<Object> sz=new ArrayList<>();
        sz.add(spu.getSpu_Name());
        sz.add(spu.getTitle());
        sz.add(spu.getClass_id());
        sz.add(spu.getBrand_id());
        sz.add(spu.getSpuimg());
        sz.add(spu.getSpu_Info());
        sz.add(spu.getPacking());
        sz.add(spu.getService());
        sz.add(spu.getTakedown());
        sz.add(spu.getUpdateTime());
        sz.add(spu.getSpu_id());
        return executeUpdate(sql,sz);
    }
}
